Access params[] and local attributes in static class as *_filter
        Posted  
        
            by Mattias
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Mattias
        
        
        
        Published on 2010-05-26T12:58:35Z
        Indexed on 
            2010/05/26
            13:01 UTC
        
        
        Read the original article
        Hit count: 343
        
ruby-on-rails
|ruby
Hi!
I'm trying to refactor some code and move some of my before_filter's from the controller to a class.
Before:
class UsersController < ApplicationController
   before_filter :find_user
   def find_user
      @user = User.find(params[:id])
   end
end
...
After
class FindUserFilter
    def self.filter(controller)
        @user = User.find(params[:id])
    end
end
class UsersController < ApplicationController
   before_filter FindUserFilter
end
class GuestbookController < ApplicationController
   before_filter FindUserFilter
end
This results in an error because neither params[:id] nor @user is available/definable in the FindUserFilter-class.
Any idea how to fix this?
© Stack Overflow or respective owner